
(function(){
  ADF.policy.BootstrapContainerLayoutPolicy = function(){
    ADF.policy.BootstrapContainerLayoutPolicy.superclass.constructor.call(this);
  }

  //static properties
  COM.util.defineClass(ADF.policy.BootstrapContainerLayoutPolicy, COM.gef.XYLayoutEditPolicy, {
    /*createChildEditPolicy: function(child) {

    },*/
    showLayoutTargetFeedback: function(request) {
      //展示目标反馈
      var RequestConstants = COM.gef.RequestConstants;
      if (RequestConstants.REQ_CREATE == (request.getType())){
        var model = request.getNewObject();
        if(model instanceof ADF.model.BootstrapRowModel) {
          this.getHost().getFigure().getWidget().showFeedback(model.getLayoutData());
        }
      }
    },
    eraseLayoutTargetFeedback: function(request) {
      //擦除目标反馈
      this.getHost().getFigure().getWidget().eraseFeedback();
    },
    createChangeConstraintCommand: function(request, child, rect){
      //COM.draw2d.PositionConstants WEST EAST
      if(request.getResizeDirection() != COM.draw2d.PositionConstants.SOUTH) {
          return COM.gef.UnexecutableCommand.INSTANCE;
      }
      if(rect.height > this.getHost().getFigure().getSize().height) {
          return COM.gef.UnexecutableCommand.INSTANCE;
      }
      var model = child.getModel();
      return COM.gef.Command.createCommand({
        label: 'change bounds',
        execute: function(){
          this.old = model.getLayoutData();
          model.setLayoutData(rect.height);
        },
        undo: function(){
          model.setLayoutData(this.old);
        }
      });

    },
    getCreateCommand: function(request){
      if(!(request.getNewObject() instanceof ADF.model.BootstrapRowModel)) {
        return COM.gef.UnexecutableCommand.INSTANCE;
      }
      return COM.gef.Command.createCommand({
        label: 'create child',
        child: request.getNewObject(),
        parent: this.getHost().getModel(),
        figure: this.getHost().getFigure(),
        execute: function(){//
          this.parent.addChild(this.child);
        },
        undo: function(){
          this.parent.removeChild(this.child);
        }
      });
    },
    createAddCommand: function(request, child, height) {
      if(!(child.getModel() instanceof ADF.model.BootstrapRowModel)) {
        return COM.gef.UnexecutableCommand.INSTANCE;
      }
      var command = new COM.gef.CompoundCommand(), deleteCommand = null, addCommand = null;
      deleteCommand = COM.gef.Command.createCommand({
        label: 'delete model',
        parentModel: child.getParent().getModel(),
        childModel:child.getModel(),
        oldBounds: child.getModel().getLayoutData(),
        execute: function(){
          this.parentModel.removeChild(this.childModel);
        },
        undo: function(){
          this.parentModel.addChild(this.childModel);
          this.childModel.setLayoutData(this.oldBounds);
        }
      });
      addCommand = COM.gef.Command.createCommand({
        label: 'add model',
        parentModel: this.getHost().getModel(),
        childModel:child.getModel(),
        execute: function(){
          this.parentModel.addChild(this.childModel);
          this.childModel.setLayoutData(height);
        },
        undo: function(){
          this.parentModel.removeChild(this.childModel);
        }
      });
      command.add(deleteCommand);
      command.add(addCommand);
      return command.unwrap();
    },
    getConstraintFor: function(request, child) {
      var ld = child.getModel().getLayoutData();
      if(Object.prototype.toString.call(ld) == "[object Number]"){
        return ld;
      }
      return 60;
    }
  });

})();
